package cn.psh.controller;


import cn.psh.AjaxResult;
import cn.psh.domain.Product;
import cn.psh.feignclient.PFeiginClient;
import cn.psh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private PFeiginClient pFeiginClient;

    @PostMapping
    public AjaxResult addProduct(@RequestBody Product product){
        try {
            final Product product1 = productService.loadByName(product.getName());
            if(product1==null){
           Product p=productService.add(product);
           product.setId(p.getId());
            }
            return pFeiginClient.getById(product);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("保存失败");
        }

    }

}
